/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DAO.MovieJpaController;
import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.PreexistingEntityException;
import Entidad.Movie;
import grlu.Main;

/**
 *
 * @author USUARIO
 */
public class MovieControl {    
    private MovieJpaController JPAMovie;
    private String aux;
    
    public MovieControl(){
        JPAMovie = new MovieJpaController(Main.emf);
        aux = "";
    }
    
    
    public Movie findMovie(String name){
        return JPAMovie.findMovie(name);
    }    
    
    
    public int deleteMovie(Movie movie){
        try {
            JPAMovie.destroy(movie.getName());
        } catch (NonexistentEntityException ex) {
            return 1;
        }
        return 0;
    }
    
    public int createMovie(Movie movie){
        setUnknowns(movie);
        try {
            JPAMovie.create(movie);
        } catch (PreexistingEntityException ex) {
            return 2;
        } catch (Exception ex) {
            return 1;
        }
        return 0;
    }
    
    public void setUnknowns(Movie movie){
        int auxYear = (movie.getYearOfCreation() == null)? 1970 : movie.getYearOfCreation();
        movie.setYearOfCreation(auxYear);
        aux = (movie.getCover() == null)? "Unknown" : movie.getCover();
        movie.setCover(aux);
        aux = (movie.getPlotKeywords() == null)? "Unknown" : movie.getPlotKeywords();
        movie.setPlotKeywords(aux);
        aux = (movie.getGenre() == null)? "Unknown" : movie.getGenre();
        movie.setGenre(aux);
        float auxDuration = (movie.getDuration() == null)? new Float(0) : movie.getDuration();
        movie.setDuration(auxDuration);
    }
    
}
